import {request} from '../../network/index'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		active: 0,
		musicTag:[],
		first:[],
		musicListContent:[],
		goodTag:[],
		goodMusic:[],
		tag:'',
		offset:12
	},
	async onChange(event) {
		if(event.detail.name !== '精品' && event.detail.name !== '推荐'){
			this.getMusicMenu(event.detail.name)
			this.setData({
				tag:event.detail.name
			})
		}else if(event.detail.name === '精品'){
			//精品歌单列表
			let goodTag = await request('/playlist/highquality/tags')
			this.goodMusic()
			this.setData({
				goodTag:goodTag.data.tags
			})
		}
	
	},
	change(item){
		console.log(item.detail);
		this.selectComponent('#C1').onClose()
		this.goodMusic(item.detail)
	},
	//精品歌单
	 async goodMusic(cat='全部',num=12){
		let goodMusic = await request('/top/playlist/highquality',{cat,limit:num})
		this.setData({
			goodMusic:goodMusic.data.playlists
		})
	 },
	//根据标签获取歌单
	async getMusicMenu(cat,num=12,offset=1){
		let categoryMusiclist = await request('/top/playlist',{cat,limit:num,offset})
		this.setData({
			musicListContent:categoryMusiclist.data.playlists
		})
	},
	//点击音乐标签菜单
	toMusicMenuTags(){
		wx.navigateTo({
			url: '/pages/musicTags/musicTags',
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad:async function (options) {
	
		let musicTag = await request('/playlist/hot')
		let plazaContent = await request('/personalized')
		this.setData({
			musicTag:musicTag.data.tags,
			first:plazaContent.data.result,
		})

	},
	toDetail(event){
		let id = event.currentTarget.dataset.id
		wx.navigateTo({
			url: `/pages/musciListDetial/musciListDetial?id=${id}`
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		let tags = wx.getStorageSync('tags')
		this.setData({
			musicTag:tags
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom:async function () {
		this.data.offset +=12
		this.setData({
			offset:this.data.offset
		})
		
			let categoryMusiclist = await request('/top/playlist',{cat:this.data.tag,limit:12,offset:this.data.offset})
			this.setData({
				musicListContent:[...this.data.musicListContent,...categoryMusiclist.data.playlists]
			})
	
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})